package com.gbei.center.utils;

import com.alibaba.fastjson.JSONObject;
import com.gbei.center.req.open.ReqAreaCode;
import com.gbei.center.req.open.ReqThirdpartyPlaceOrder;
import com.gbei.center.req.open.ReqUserCommon;
import org.apache.poi.ss.formula.functions.T;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;

/**
 * @Description:
 * @date 2019年05月23日
 */
@Component
public class RestTemplateClient {

	private static Logger log = LoggerFactory.getLogger(RestTemplateClient.class);
	
	@Autowired
	private RestTemplate restTemplate;

	
	/**
	 * 会员注册/查询/修改/信息补全
	 * @param req
	 * @return
	 */
	public String getUserInfo(ReqUserCommon req){
		HttpHeaders headers = new HttpHeaders();
		headers.setContentType(MediaType.valueOf("application/json;charset=UTF-8"));
		HttpEntity<String> jsonStr = new HttpEntity<>(JSONObject.toJSON(req).toString(), headers);
		log.info("@@@@@@@@@ 调用内部接口的参数:{}",jsonStr);
		String response = restTemplate.postForObject("http://thirdparty/user/userCommon", jsonStr, String.class);
		log.info("@@@@@@@@@ 调用内部接口的返回结果:{}",response);
		return response;
	}

	/**
	 * 订单下单/修改/查询/退款
	 * @param req
	 * @return
	 */
	public String orderCommon(ReqThirdpartyPlaceOrder req){
		HttpHeaders headers = new HttpHeaders();
		headers.setContentType(MediaType.valueOf("application/json;charset=UTF-8"));
		HttpEntity<String> jsonStr = new HttpEntity<String>(JSONObject.toJSON(req).toString(), headers);
		log.info("@@@@@@@@@ 调用内部接口的参数:{}",jsonStr);
		String response = restTemplate.postForObject("http://thirdparty/order/orderCommon", jsonStr, String.class);
		log.info("@@@@@@@@@ 调用内部接口的返回结果:{}",response);
		return response;
	}

	/**
	 * 获取京贝尔体系行政区域编码
	 * @param req
	 * @return
	 */
	public String areaCommon(ReqAreaCode req){
		HttpHeaders headers = new HttpHeaders();
		headers.setContentType(MediaType.valueOf("application/json;charset=UTF-8"));
		HttpEntity<String> jsonStr = new HttpEntity<String>(JSONObject.toJSON(req).toString(), headers);
		log.info("@@@@@@@@@ 调用内部接口的参数:{}",jsonStr);
		String response = restTemplate.postForObject("http://thirdparty/area/areaCommon", jsonStr, String.class);
		log.info("@@@@@@@@@ 调用内部接口的返回结果:{}",response);
		return response;
	}

	/**
	 * 调用内部接口通用方法
	 * @param req
	 * @return
	 */
	public <K> String common(K req, String url){
		HttpHeaders headers = new HttpHeaders();
		headers.setContentType(MediaType.valueOf("application/json;charset=UTF-8"));
		HttpEntity<String> jsonStr = new HttpEntity<>(JSONObject.toJSON(req).toString(), headers);
		log.info("@@@@@@@@@ 调用内部接口的路径:{},参数:{}",url,jsonStr);
		String response = restTemplate.postForObject(url, jsonStr, String.class);
		log.info("@@@@@@@@@ 调用内部接口的返回结果:{}",response);
		return response;
	}

}
